package com.chenfwind.bookclub.activity;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.graphics.Color;
import android.os.Bundle;
import android.app.Fragment;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import com.chenfwind.bookclub.R;

import com.chenfwind.bookclub.View.SlidingMenu;
import com.jauker.widget.BadgeView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by mufeng on 2016/3/29.
 */
public class MainActivity extends Activity {

    private TextView mChatTextView;
    private TextView mFriendTextView;
    private TextView mContactTextView;
    private ImageView mTabline;
    private SlidingMenu mLeftMenu;

    private int mScreen1_3;

    private Fragment[] mDatas;
    private RelativeLayout rlChat;
    private RelativeLayout rlFriend;
    private RelativeLayout rlContact;
    private ImageView[] imageButtons;
    private TextView[] textViews;
    private int index;
    private int currentTabIndex;
    /*下拉框*/
//    private Spinner sMenuItem;
//    private SimpleAdapter adapterMenuItem;
//    // 第一步：添加一个下拉列表项的list，这里添加的项就是下拉列表的菜单项
//    private List<Map<String, Object>> datalist;

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        //消除头
//        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);

        mLeftMenu = (SlidingMenu) findViewById(R.id.id_menu);

        init();
    }

    private void init(){
        initTabline();
        //初始化
        initView();
        //menuItem初始化
//        initMenuItem();
    }


    public void toggleMenu(View view){
        mLeftMenu.toggle();
    }

    private void initTabline() {
        //指示器随滑动改变
        mTabline = (ImageView) findViewById(R.id.id_iv_tabline);
        Display display = getWindow().getWindowManager().getDefaultDisplay();
        DisplayMetrics outMetrics = new DisplayMetrics();
        display.getMetrics(outMetrics);
        mScreen1_3 = outMetrics.widthPixels / 3;

        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mTabline.getLayoutParams();
        lp.width = mScreen1_3;
        mTabline.setLayoutParams(lp);
    }

    private void initView() {

        rlChat = (RelativeLayout) findViewById(R.id.id_rl_chat);
        rlFriend = (RelativeLayout) findViewById(R.id.id_rl_friend);
        rlContact = (RelativeLayout) findViewById(R.id.id_rl_contact);

        //给MainView 的迭代器
        ChatMainFragment chatMain = new ChatMainFragment();
        FriendMainFragment friendMain = new FriendMainFragment();
        ContactMainFragment contactMain = new ContactMainFragment();
        mDatas = new Fragment[]{chatMain , friendMain , contactMain};

        imageButtons = new ImageView[3];
        imageButtons[0] = (ImageView) findViewById(R.id.ib_chat);
        imageButtons[1] = (ImageView) findViewById(R.id.ib_friend);
        imageButtons[2] = (ImageView) findViewById(R.id.ib_contact);
        imageButtons[0].setSelected(true);

        textViews = new TextView[3];
        textViews[0] = (TextView) findViewById(R.id.id_tv_chat);
        textViews[1] = (TextView) findViewById(R.id.id_tv_friend);
        textViews[2] = (TextView) findViewById(R.id.id_tv_contact);
        textViews[0].setTextColor(0xFF45C01A);

        getFragmentManager().beginTransaction().add(R.id.id_viewpager, chatMain).add(R.id.id_viewpager ,friendMain).add(R.id.id_viewpager ,contactMain).hide(contactMain).hide(friendMain).show(chatMain).commit();

    }

    public void onTabClicked(View view) {
        LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) mTabline.getLayoutParams();
        switch (view.getId()) {
            case R.id.id_rl_chat:
                index = 0;
                lp.leftMargin = (int) (index * mScreen1_3);
                mTabline.setLayoutParams(lp);
                break;
            case R.id.id_rl_friend:
                index = 1;
                lp.leftMargin = (int) (index * mScreen1_3);
                mTabline.setLayoutParams(lp);
                break;
            case R.id.id_rl_contact:
                index = 2;
                lp.leftMargin = (int) (index * mScreen1_3);
                mTabline.setLayoutParams(lp);
                break;
        }

        if (currentTabIndex != index) {
            FragmentManager fm = getFragmentManager();
            FragmentTransaction trx = fm.beginTransaction();

            trx.hide(mDatas[currentTabIndex]);
            if (!mDatas[index].isAdded()) {
                trx.add(R.id.id_viewpager, mDatas[index]);
            }
            trx.show(mDatas[index]).commit();
        }
        imageButtons[currentTabIndex].setSelected(false);
        imageButtons[currentTabIndex].setAlpha(0.0f);
        // 把当前tab设为选中状态
        imageButtons[index].setSelected(true);
        imageButtons[index].setAlpha(1.0f);
        textViews[currentTabIndex].setTextColor(0xFF999999);
        textViews[index].setTextColor(0xFF45C01A);
        currentTabIndex = index;
    }

    //对字体初始化
    private void resetTextView() {
        mChatTextView.setTextColor(Color.BLACK);
        mFriendTextView.setTextColor(Color.BLACK);
        mContactTextView.setTextColor(Color.BLACK);
    }
}
